/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.home.dao;

import com.home.common.utils.HibernateUtil;
import com.home.mapping.Scorecard;
import com.home.mapping.ScorecardDetails;
import com.home.mapping.ScorecardDetailsResult;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author vi
 */
public class ScorecardDetailsResultDao extends BaseDao{
    public boolean deleteScorecardDetailsResultByScorecardId(Integer scorecardId) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction trans = null;
        boolean returnValue = true;
        try {
            trans = session.beginTransaction();
            session.createSQLQuery("DELETE FROM scorecard_details_result WHERE scorecard_details_result.fk_scorecard_result = :scorecardId")
                    .addEntity(ScorecardDetailsResult.class)
                    .setParameter("scorecardId", scorecardId)
                    .executeUpdate();
            trans.commit();
            
        }catch (Exception e) {
            e.printStackTrace();
            returnValue = false;
            trans.rollback();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return returnValue;
    }
    
    public List<ScorecardDetailsResult> findScorecardDetailsResultByScorecardId(Integer scorecardId) {
        List<ScorecardDetailsResult> list = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            Query query = session.createSQLQuery("SELECT * FROM scorecard_details_result WHERE scorecard_details_result.fk_scorecard_result =:scorecardId")
                    .addEntity(ScorecardDetailsResult.class)
                    .setParameter("scorecardId", scorecardId);
            list = query.list();    
                    
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return list;
    }
    
    public List<ScorecardDetailsResult> findScorecardDetailResultByScorecardIds(List<Integer> listScorecardIds) {
        List<ScorecardDetailsResult> list = new ArrayList<ScorecardDetailsResult>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            Query query = session.createSQLQuery("SELECT * FROM scorecard_details_result WHERE "
                    + "scorecard_details_result.fk_scorecard_result IN ("+StringUtils.join(listScorecardIds, ",")+")")
                    .addEntity(ScorecardDetailsResult.class);
                    
            list = query.list();
            for (ScorecardDetailsResult scDetailsResult : list) {
                Hibernate.initialize(scDetailsResult.getScorecard());                
            }
        } catch (Exception e) {
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        
        return list;
    }
}
